package xin.yangshuai.oauth202.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import xin.yangshuai.common01.entity.AuthorizationInfo;

@Controller
public class IndexController {

    @RequestMapping
    @ResponseBody
    public String index() {
        return "hello world oauth2";
    }

    @GetMapping("implicit/authorize")
    public String implicitAuthorize(String clientId, String redirectUrl, String scope, String responseType) {

        System.out.println(clientId);
        System.out.println(redirectUrl);
        System.out.println(scope);
        System.out.println(responseType);

        return "redirect:" + redirectUrl + "?accessToken=123456&tokenType=bearer&expiresIn=3600";
    }

    @GetMapping("authorizationCode/authorize")
    public String authorizationCodeAuthorize(String clientId, String redirectUrl, String scope, String responseType, String state) {

        System.out.println(clientId);
        System.out.println(redirectUrl);
        System.out.println(scope);
        System.out.println(responseType);
        System.out.println(state);

        return "redirect:" + redirectUrl + "?code=123qwe&state=" + state;
    }

    @ResponseBody
    @GetMapping("authorizationCode/accessToken")
    public AuthorizationInfo authorizationCodeAccessToken(String clientId, String clientSecret, String code, String grantType) {

        System.out.println(clientId);
        System.out.println(clientSecret);
        System.out.println(code);
        System.out.println(grantType);

        AuthorizationInfo authorizationInfo = new AuthorizationInfo();
        authorizationInfo.setAccessToken("accessToken1234567890");
        authorizationInfo.setExpiresIn(1000);
        authorizationInfo.setRefreshToken("refreshToken1234567890");
        authorizationInfo.setRefreshExpiresIn(0);
        authorizationInfo.setOpenId("openId001");
        authorizationInfo.setScope("01,02");
        authorizationInfo.setTokenType("bearer");

        return authorizationInfo;
    }

    @ResponseBody
    @GetMapping("authorizationCode/refreshToken")
    public AuthorizationInfo authorizationCodeRefreshToken(String clientId, String clientSecret, String grantType, String refreshToken) {

        System.out.println(clientId);
        System.out.println(clientSecret);
        System.out.println(grantType);
        System.out.println(refreshToken);

        AuthorizationInfo authorizationInfo = new AuthorizationInfo();
        authorizationInfo.setAccessToken("accessToken1234567890");
        authorizationInfo.setExpiresIn(1000);
        authorizationInfo.setRefreshToken("refreshToken1234567890");
        authorizationInfo.setRefreshExpiresIn(0);
        authorizationInfo.setOpenId("openId001");
        authorizationInfo.setScope("01,02");
        authorizationInfo.setTokenType("bearer");

        return authorizationInfo;
    }

    @ResponseBody
    @GetMapping("passwordCredentials/accessToken")
    public AuthorizationInfo passwordCredentialsAccessToken(String clientId, String clientSecret, String username, String password, String grantType) {

        System.out.println(clientId);
        System.out.println(clientSecret);
        System.out.println(username);
        System.out.println(password);
        System.out.println(grantType);

        AuthorizationInfo authorizationInfo = new AuthorizationInfo();
        authorizationInfo.setAccessToken("accessToken1234567890");
        authorizationInfo.setExpiresIn(1000);
        authorizationInfo.setRefreshToken("refreshToken1234567890");
        authorizationInfo.setRefreshExpiresIn(0);
        authorizationInfo.setOpenId("openId001");
        authorizationInfo.setScope("01,02");
        authorizationInfo.setTokenType("bearer");

        return authorizationInfo;
    }

    @ResponseBody
    @GetMapping("clientCredentials/accessToken")
    public AuthorizationInfo clientCredentialsAccessToken(String clientId, String clientSecret, String grantType) {

        System.out.println(clientId);
        System.out.println(clientSecret);
        System.out.println(grantType);

        AuthorizationInfo authorizationInfo = new AuthorizationInfo();
        authorizationInfo.setAccessToken("accessToken1234567890");
        authorizationInfo.setExpiresIn(1000);
        authorizationInfo.setScope("01,02");
        authorizationInfo.setTokenType("bearer");

        return authorizationInfo;
    }

}